package com.atguigu.day07;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoFlatMapFunction;
import org.apache.flink.util.Collector;

// 查询流的实现
public class Example1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<ClickEvent> clickSource = env.addSource(new ClickSource());

        // 查询字符串的流
        DataStreamSource<String> queryStream = env.socketTextStream("localhost", 9999);

        clickSource.keyBy(r -> r.url)
                // 在将queryStream广播之前，要保证queryStream的并行度是1
                // 也就是要将数据按顺序广播出去
                .connect(queryStream.setParallelism(1).broadcast())
                .flatMap(new Query())
                .print();

        env.execute();
    }

    public static class Query implements CoFlatMapFunction<ClickEvent, String, ClickEvent> {
        // 全局变量用来保存查询字符串
        private String query = "";
        @Override
        public void flatMap1(ClickEvent value, Collector<ClickEvent> out) throws Exception {
            if (value.username.equals(query)) out.collect(value);
        }

        @Override
        public void flatMap2(String value, Collector<ClickEvent> out) throws Exception {
            query = value;
        }
    }
}
